<!DOCTYPE html>
<HTML lang="en">
<HEAD>
    <title> Using loadimage in Baby X </title>
    <meta charset="UTF-8">
        
    <link href="prism.css" rel="stylesheet">
<script src="microlight.js"> </script>
<script src="prism.js"> </script>
<style>
.microlight {
    font-family : monospace;
    white-space : pre;
    background-color : white;
}
    
BODY {
    width:50em;
    margin-left:5em;
    background-color:#c0c0ff;
}

P {
   width : 50em;
}

pre {
   background-color:white;
}
</style>
</HEAD>

<BODY>
    <script src="prism.js"></script>
    <A href ="https://github.com/MalcolmMcLean"> <IMG src = "Cartoon_Owl_clip_art.png" alt="Malcolm's github site">  </A>
    <A href ="index.html"> <IMG src = "babyxrc_logo.svg" alt="Baby X Resource compiler" width = "64" height = "62">  </A>
    &nbsp;&nbsp;
    <IMG src = "babyxrc-banner.svg" width = "256" height = "62" alt = "Baby X RC banner">
<H1> Using loadimage in Baby X </H1>
<P>
The Baby X resource compiler comes with a powerful loadimage() function which you will want to take for your own baby programs if you want to allow the user to specify and image to load a runtime.
</P> 

<H3> The image load function </H3>
<P>
The image loader which comes with the Baby X resource compiler is
powerful and easy to use. It will load most common image files.
</P>
<pre><code class="language-c">
    /*
    load an image as 32 bit rgba.
          
    Params:
           fname - the name of the file to load.
           width - return pointer for image width.
           height - return pointer for image height.
           err - return for error code.
    Returns:
           the image as a 32 bit rgba buffer, 0 on fail.
                
    */
    unsigned char *loadrgba(char *fname, int *width, int *height, int *err)
    {
      int fmt;
      unsigned char *answer = 0;

      if(err)
        *err = 0;

      fmt = getformat(fname);

      switch(fmt)
      {
      case FMT_UNKNOWN:
        if(err)
          *err = -3;
        return 0;
      case FMT_JPEG:
        answer = loadasjpeg(fname, width, height);
        break;
      case FMT_PNG:
        answer =  loadaspng(fname, width, height);
        break;
      case FMT_BMP:
        answer =  loadasbmp(fname, width, height);
        break;
      case FMT_GIF:
        answer =  loadasgif(fname, width, height);
        break;
      case FMT_TIFF:
         answer = loadastiff(fname, width, height);
         break;
      case FMT_SVG:
          answer = loadassvg(fname, width, height);
          break;
      }
      if(!answer)
        if(err)
          *err = -1;

      return answer;
    }
    

</code></pre>

<P>
And of course I do not need to insult your intelligence by showing an example of how to use it.
</P>

<P>
Dependencies:
</P>
<UL>
    <LI>bmp.c</LI>
    <LI>jpeg.c</LI>
    <LI>gif.c
    <UL>
        <LI>rbtree.c</LI>
    </UL>
    <LI>lodepng.c</LI>
    <LI>loadtiff.c</LI>
    <LI>nanosvg.h</LI>
    <LI>nanosvgrast.h</LI>
</UL>
<P>
And the C files all have associated headers. So you need to take those files and drop them into your Baby X program, as well as loadimage.c.
</P>
<P>
lodepng.c is maintained <A href = "https://github.com/lvandeve/lodepng">here</A>. <BR>
nanosvg.h nanosvrast.h <A href = "https://github.com/memononen/nanosvg">here</A>. <BR>
<BR>
All other components are by <A href="https://github.com/MalcolmMcLean"><IMG src="Cartoon_Owl_clip_art.png" alt="Malcolm's github site"></A>
</P>
<P>
And this function is heart of the resource compiler. It's what most people will use it for.
</P>
</BODY>
</HTML>
